package com.atguigu.multiReading;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Practice {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        int[] array=new int[10000000];
        for(int i=0;i<array.length;i++){
            array[i]=(int)(Math.random()*100)+1;
        }
        int sum=0;
        long start=System.currentTimeMillis();
        FutureTask<Integer> futureTask1=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum1=0;
                for(int i=0;i<array.length;i++){
                    if((i&1)==0){
                        sum1+=array[i];
                    }
                }
                return sum1;
            }
        });
        Thread thread=new Thread(futureTask1);
        thread.start();
        thread.join();
        System.out.println("偶数位总和为:"+futureTask1.get());
        for(int i=0;i<array.length;i++){
            if((i&1)!=0){
                sum+=array[i];
            }
        }
        System.out.println("奇数位总和为"+sum);
        sum+=futureTask1.get();
        System.out.println("总和为"+sum);
        long end=System.currentTimeMillis();
        long time=end-start;
        System.out.println("所用时间为"+time);
        };

    }
